from flask import request
from flask_restful import Resource, marshal_with

from libs.login import login_required
from services.area_init_service import AreaService
from controllers.mini_app import api
from fields.area_fields_def import area_fields



class AreaApi(Resource):

    # @login_required
    @marshal_with(area_fields)
    def get(self):
        """获取区域列表"""
        parent_code = request.args.get("parent_code", None, int)
        area_type = request.args.get("area_type", 1, int)
        return AreaService.get_areas_by_parent(parent_code, area_type)
    
class AreaInfoApi(Resource):
    @login_required
    @marshal_with(area_fields)
    def get(self, area_code:str):
        """获取区域信息"""
        areaCode = AreaService.format(area_code)
        if area_code in [0,-1]:
            raise BadRequest("无效的区域编码")
        return AreaService.get_area_by_code(areaCode)

api.add_resource(AreaInfoApi, "/area/<string:area_code>")
api.add_resource(AreaApi, "/area")